<html>
<head><meta charset="utf-8"><title>Two lifetimes shall be identical, but Rust doesn&#x27;t let me · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Two.20lifetimes.20shall.20be.20identical.2C.20but.20Rust.20doesn&#x27;t.20let.20me.html">Two lifetimes shall be identical, but Rust doesn&#x27;t let me</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="238049755"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Two%20lifetimes%20shall%20be%20identical%2C%20but%20Rust%20doesn%27t%20let%20me/near/238049755" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Colin Finck <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Two.20lifetimes.20shall.20be.20identical.2C.20but.20Rust.20doesn&#x27;t.20let.20me.html#238049755">(May 09 2021 at 17:41)</a>:</h4>
<p>Hi! In one of my projects, I'm defining a trait <code>New</code> to address all objects with a common <code>fn new</code>. This <code>new</code> function takes an <code>&amp;str</code> parameter, so lifetimes come into play. One implementation wants to store that <code>&amp;str</code> parameter in its struct, so the struct also needs a lifetime parameter. On top of that, I don't want trait bounds on <code>New</code> to deal with the lifetime, so I'm definining the lifetime only for the function and not for the entire trait (<code>fn new&lt;'a&gt;</code> instead of <code>trait New&lt;'a&gt;</code>).</p>
<p>My current code looks like this and doesn't work :D</p>
<div class="codehilite"><pre><span></span><code>trait New {
    fn new&lt;&#39;a&gt;(value: &amp;&#39;a str) -&gt; Self;
}

struct Obj&lt;&#39;n&gt; {
    value: &amp;&#39;n str,
}

impl&lt;&#39;n&gt; New for Obj&lt;&#39;n&gt; {
    fn new&lt;&#39;a&gt;(value: &amp;&#39;a str) -&gt; Self {
        Self { value }
    }
}
</code></pre></div>
<p>As per Rust rules, I need to specify a lifetime on every <code>impl</code> for <code>Obj</code>. Due to the defined trait, I need to specify a different lifetime for <code>fn new</code>.<br>
But now the Rust compiler complains that both lifetimes are not identical.<br>
I can't change anything in the <code>fn new</code> line though, because then the Rust compiler complains that I'm not implementing the trait correctly.</p>
<p>What am I missing?</p>



<a name="238050097"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Two%20lifetimes%20shall%20be%20identical%2C%20but%20Rust%20doesn%27t%20let%20me/near/238050097" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Two.20lifetimes.20shall.20be.20identical.2C.20but.20Rust.20doesn&#x27;t.20let.20me.html#238050097">(May 09 2021 at 17:47)</a>:</h4>
<p>You cannot implement that trait by storing borrowed data in <code>Self</code></p>



<a name="238050145"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Two%20lifetimes%20shall%20be%20identical%2C%20but%20Rust%20doesn%27t%20let%20me/near/238050145" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Two.20lifetimes.20shall.20be.20identical.2C.20but.20Rust.20doesn&#x27;t.20let.20me.html#238050145">(May 09 2021 at 17:48)</a>:</h4>
<p>You can change the trait to be generic over <code>'a</code> instead of the <code>new</code> method, then you should be able to implement it</p>



<a name="238050303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Two%20lifetimes%20shall%20be%20identical%2C%20but%20Rust%20doesn%27t%20let%20me/near/238050303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Colin Finck <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Two.20lifetimes.20shall.20be.20identical.2C.20but.20Rust.20doesn&#x27;t.20let.20me.html#238050303">(May 09 2021 at 17:51)</a>:</h4>
<p><span aria-label="light bulb" class="emoji emoji-1f4a1" role="img" title="light bulb">:light_bulb:</span></p>



<a name="238050408"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Two%20lifetimes%20shall%20be%20identical%2C%20but%20Rust%20doesn%27t%20let%20me/near/238050408" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Colin Finck <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Two.20lifetimes.20shall.20be.20identical.2C.20but.20Rust.20doesn&#x27;t.20let.20me.html#238050408">(May 09 2021 at 17:53)</a>:</h4>
<p>Thanks a lot! That was indeed the part I missed.<br>
Now I can also deduce a rule for myself when to put type parameters in the function and when to put them in the trait itself :)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>